package controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import session_beans.AirTransportSessionBean;
import session_beans.AirTransportSessionRemote;
import entity_beans.User;

/**
 * Servlet implementation class for Servlet: LogoutServlet
 *
 */
public class LogoutServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
	
	private static final long serialVersionUID = 497255795584746404L;
	
	
	private AirTransportSessionRemote airTransportSession;
	
	
	@Override
	public void init() throws ServletException {
		super.init();
		
		airTransportSession = new AirTransportSessionBean();
	}
	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}  	
	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		
		User user = (User) session.getAttribute("user");
		
		if (user.isLoggedIn() == true) {
			user.logoff();
			airTransportSession.logoutUser(user);
			
			session.invalidate();

			response.sendRedirect("index.jsp");
		}
		else {
			response.sendRedirect("index.jsp");
		}
	}   	  	    
}
